package ru.ifmo.ipimenov.ewf_svn.standalone.dao.impl;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import ru.ifmo.ipimenov.ewf_svn.standalone.dao.DocumentDao;
import ru.ifmo.ipimenov.ewf_svn.standalone.dao.StateDao;
import ru.ifmo.ipimenov.ewf_svn.standalone.dao.RulesDao;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.*;
import ru.ifmo.ipimenov.ewf_svn.standalone.util.HibernateUtil;

import java.util.List;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class RulesDaoHibernate implements RulesDao {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(DocumentDaoHibernate.class);

    public List<Rules> list(Document document) {
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();

        List<Rules> l = s.createCriteria(Rules.class).add(Restrictions.eq("document", document)).list();
        s.getTransaction().commit();

        return l;
    }

    public void save(Rules r) {
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();

        s.save(r);

        s.getTransaction().commit();
    }

    public void delete(Rules r) {
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();

        s.delete(r);

        s.getTransaction().commit();

    }

}